var xml2js = require('xml2js');
import { MySql } from "../DB/MySql";
var fs = require('fs');

export class XMLOperation {

    public jsonBuilder: any;
    private mysqlDB: any;




    /**
       * Constructor
       *
       * @class PGApiController
       * @constructor
       */
    constructor() {

        this.jsonBuilder = xml2js.parseString;
        this.mysqlDB = new MySql();
    }


    public Parser(xmlStr: any) {
        let retData = [];
        let pageInfo = {};
        this.jsonBuilder(xmlStr, { explicitArray: false }, function (err, result) { 
            pageInfo = result.rss.list.$;
            let videos = result.rss.list.video; 
            for (var i = 0; i < videos.length; i++) {  
                try { 
                    let data = videos[i];
                    let last = data.last;
                    let id = data.id;
                    let tid = data.tid;
                    let name = data.name;
                    let type = data.type;
                    let pic = data.pic;
                    let lang = data.lang;
                    let area = data.area;
                    let year = data.year;
                    let state = data.state;
                    let note = data.note;
                    let actor = data.actor;
                    let director = data.director;
                    let des = data.des;
                    let dataInfo = {};
    
                    dataInfo["last"] = last;
                    dataInfo["tid"] = tid;
                    dataInfo["name"] = name;
                    dataInfo["type"] = type;
                    dataInfo["pic"] = pic;
                    dataInfo["lang"] = lang;
                    dataInfo["area"] = area;
                    dataInfo["year"] = year;
                    dataInfo["state"] = state;
                    dataInfo["note"] = note;
                    dataInfo["actor"] = actor;
                    dataInfo["director"] = director;
                    dataInfo["des"] = des;
                    dataInfo["collect"] = [];
                    let dds = data.dl.dd;   
                    if (Array.isArray(dds)) {
                        for (var j = 0; j < dds.length; j++) {
                            let dd = dds[j]; 
                            if (dd._ != undefined && dd._ != null) { 
                                let files = dd._.split('#');
                                if (files.length > 0) {  
                                    files.forEach(element => { 
                                    let sp = element.split("$"); 
                                    if(sp.length>1)
                                    {
                                        let fileName = sp[0];
                                        let fileurl = sp[1];   
                                        let collect = { type: dd.$.flag, name: fileName, url: fileurl };
                                        dataInfo["collect"].push(collect); 
                                    }
                                    else{
                                        let fileName = "TV";
                                        let fileurl = sp[0];
                                        let collect = { type: dd.$.flag, name: fileName, url: fileurl };
                                        dataInfo["collect"].push(collect); 
                                    }
                                    });
                                }
                            }
    
    
                        }
                    }
                    else {
                        if (dds._ != undefined && dds._ != null) {
                            let files = dds._.split('#');
                            if (files.length > 0) {
                                files.forEach(element => {
                                    let sp = element.split("$");
                                    
                                    if(sp.length>1)
                                    {
                                        let fileName = sp[0];
                                        let fileurl = sp[1];
                                        let collect = { type: dds.$.flag, name: fileName, url: fileurl };
                                        dataInfo["collect"].push(collect);
                                    }
                                    else{
                                        let fileName = "TV";
                                        let fileurl = sp[0];
                                        let collect = { type: dds.$.flag, name: fileName, url: fileurl };
                                        dataInfo["collect"].push(collect);
                                    }
                                 
                                });
                            }
                        }
                    } 
                    retData.push(dataInfo);
                } catch (error) {
                     console.error(error);
                }

            

            }
        });
        let jsonData = {};
        jsonData["video"] = retData;
        jsonData["page"] = pageInfo;
        return jsonData;
    }
}